<?php

namespace App\Http\Controllers\Api;


use App\Models\Bubble;
use App\Models\Prop;
use App\Models\Role;
use App\Models\Scene;
use App\Models\Category;
use App\Models\Picture;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Validate\api\MyValidate;
use App\Common\ConstCode;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;

class MyController extends BaseController
{
    public $successStatus=200;
    protected $bubble;
    protected $prop;
    protected $role;
    protected $scene;
    protected $category;
    protected $v;
    /**
     * api注册
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */

    public function __construct(Bubble $bubble,Prop $prop,Role $role,Scene $scene,Category $category,MyValidate $v)
    {
        $this->bubble = $bubble;
        $this->prop = $prop;
        $this->role = $role;
        $this->scene = $scene;
        $this->category = $category;
        $this->v = $v;

    }


    public function upload_lists(Request $request){
        $this->v->validate('lists');
        $data=$request->input();
        $data['page'] = $request->input('page',ConstCode::PAGE);
        $data['pageSize']  = $request->input('pageSize',ConstCode::PER_PAGE);
        $data['title']  = $request->input('title','');
        $data['category']  = $request->input('category','');
        $res=[];
        $member_id = Auth::guard('api')->user()->member_id;
        switch($data['type']){
            case 1:
                //场景
                $res=$this->scene->my_lists($data,$member_id);
                break;
            case 2:
                //角色
                $data['tag']=$request->input('tag','');
                $res=$this->role->my_lists($data,$member_id);
                break;
            case 3:
                //道具
                $res=$this->prop->my_lists($data,$member_id);
                break;
            case 4:
                //气泡
                $res=$this->bubble->my_lists($data,$member_id);
                break;
            default:
                break;
        }
        if($res){
            return response()->json(['code'=>1,'data'=>$res]);
        }else{
            return response()->json(['code'=>0,'msg'=>'数据错误']);
        }

    }

    public function works_upload(Request $request){
        $this->v->validate('upload');
        $data=$request->input();
        $res=false;
        $member_id = Auth::guard('api')->user()->member_id;
        switch($data['type']){
            case 1:
                //场景
                $res=$this->scene->works_upload($data,$member_id);
                break;
            case 2:
                //角色
                $data['tag']=$request->input('tag','');
                //判断标签完整度
                if($data['category']==1 || $data['category']==4){
                    if(count(explode(',',$data['tag']))!=3){
                        return response()->json(['code'=>0,'msg'=>'请选择完整标签']);
                    }
                }else{
                    if(count(explode(',',$data['tag']))!=1){
                        return response()->json(['code'=>0,'msg'=>'请选择完整标签']);
                    }
                }
                //判断多视图完整度
                if(count($data['data'])<5) return response()->json(['code'=>0,'msg'=>'每种角度请至少上传一张图片']);
                $res=$this->role->works_upload($data,$member_id);
                break;
            case 3:
                //道具
                $res=$this->prop->works_upload($data,$member_id);
                break;
            default:
                break;
        }

        if($res){
            return response()->json(['code'=>1,'msg'=>'添加成功，等待审核']);
        }else{
            return response()->json(['code'=>0,'msg'=>'数据错误']);
        }
    }


}
